/**
 * 
 */
package com.paypal.melearning.ui;



import java.util.Arrays;

import org.springframework.beans.factory.annotation.Autowired;

import com.paypal.melearning.domain.Pin;
import com.paypal.melearning.service.UserService;
import com.paypal.melearning.ui.component.BoardComponent;
import com.paypal.melearning.ui.component.PinFieldFactory;
import com.vaadin.data.util.BeanItem;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Component;
import com.vaadin.ui.Form;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Panel;
import com.vaadin.ui.PopupView;
import com.vaadin.ui.PopupView.PopupVisibilityEvent;
import com.vaadin.ui.PopupView.PopupVisibilityListener;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;
import com.vaadin.ui.themes.BaseTheme;

/**
 * @author prakrishnan
 * 
 */
public class AddPinUI extends VerticalLayout implements PopupVisibilityListener {

	private Panel curWindow;
	/**
	 * 
	 */
	private static final long serialVersionUID = -7365577783330053790L;
	
	@Autowired
	private UserService usrService = new UserService();
	
	private Pin newPin;

	public AddPinUI(Panel curWindow) {
		setSpacing(true);
		newPin = new Pin();
		this.curWindow = curWindow;
		BeanItem<Pin> pinItem = new BeanItem<Pin>(newPin); // item from POJO

		Component addPinForm = getAddPinUI(pinItem);

		PopupView popup = new PopupView("Add Pin", addPinForm);
		popup.setHideOnMouseOut(true);
		popup.addListener(this);

		addComponent(popup);

	}

	private Component getAddPinUI(BeanItem<Pin> personItem) {
		final Form addPinForm = new Form();
		addPinForm.setCaption("Add Pin");
		addPinForm.setWriteThrough(false); // we want explicit 'apply'
		addPinForm.setInvalidCommitted(false); // no invalid values in datamodel

		// FieldFactory for customizing the fields and adding validators
		addPinForm.setFormFieldFactory(new PinFieldFactory());
		addPinForm.setItemDataSource(personItem); // bind to POJO via BeanItem

		// Determines which properties are shown, and in which order:
		addPinForm.setVisibleItemProperties(Arrays.asList(new String[] {
				"pinName", "contentURL", "type", "description" }));

		// The cancel / apply buttons
		HorizontalLayout buttons = new HorizontalLayout();
		buttons.setSpacing(true);
		Button discardChanges = new Button("Discard changes",
				new Button.ClickListener() {
			/**
			 * 
			 */
			private static final long serialVersionUID = -148920049370529474L;

			public void buttonClick(ClickEvent event) {
				addPinForm.discard();
			}
		});
		discardChanges.setStyleName(BaseTheme.BUTTON_LINK);
		buttons.addComponent(discardChanges);
		buttons.setComponentAlignment(discardChanges, Alignment.MIDDLE_LEFT);

		Button apply = new Button("Apply", new Button.ClickListener() {
			/**
			 * 
			 */
			private static final long serialVersionUID = 6761901350957298624L;

			public void buttonClick(ClickEvent event) {
//				newPin = new Pin();
				addPinForm.commit();
				usrService.addPin(newPin, null);
				curWindow.removeAllComponents();
				curWindow.addComponent(new BoardComponent( usrService.getDefaultBoard()));
			}
		});
		buttons.addComponent(apply);
		addPinForm.getFooter().addComponent(buttons);
		addPinForm.getFooter().setMargin(false, false, true, true);

		return addPinForm;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.vaadin.ui.PopupView.PopupVisibilityListener#popupVisibilityChange
	 * (com.vaadin.ui.PopupView.PopupVisibilityEvent)
	 */
	public void popupVisibilityChange(PopupVisibilityEvent event) {
		if (!event.isPopupVisible()) {
			// getWindow().showNotification("Popup closed");
			Window.Notification n = new Window.Notification("POJO state",
					Window.Notification.TYPE_TRAY_NOTIFICATION);
			n.setPosition(Window.Notification.POSITION_CENTERED);
			n.setDescription("Pin name: " + newPin.getPinName()
					+ "<br/>Pin URL: " + newPin.getContentURL()
					+ "<br/>Pin Description: " + newPin.getDescription()
					+ "<br/>Pin Type: " + newPin.getType());
//			getWindow().showNotification(n);
			getApplication().getMainWindow().requestRepaintAll();

		}

	}

}
